#include "ScrollingBackground.h"
#include "ShooterObject.h"

void ScrollingBackground::load(std::unique_cast<LoaderParams> const &pParams)
{
    ShooterObject::load(std::move(pParams));
    m_scrollSpeed = pParams->getAnimSpeed();

    m_scrollSpeed = 1;

    m_srcRect1.x = m_srcRect1.y = 0;
    m_destRect1.x = m_position.getX();
    m_destRect1.y = m_position.getY();

    m_srcRect1.w = m_destRect1.w = m_srcRect2Width = m_destRectWidth = m_width;
    m_srcRect1.h = m_destRect1.h = m_height;


    m_srcRect2.x = m_srcRect2.y = 0;
    m_destRect2.x = m_position.getX() + m_width;
    m_destRect2.y = m_position.getY();

    m_srcRect2.w = m_destRect2.w = m_srcRect2Width = m_destRect2Width = 0;
    m_srcRect2.h = m_destRect2.h = m_height;
}

void ScrollingBackground::draw()
{
    SDL_RenderCopyEx(TheGame::Instance()->getRender(),
            TheTextureManager::Instance()->getTextureMap()[m_textureID],
            &m_srcRect1, &m_desctREct1, 0, 0, SDL_FLIP_NONE);

    SDL_RenderCopyEx(TheGame::Instance()->getRender(),
            TheTextureManager::Instance()->getTextureMap()[m_textureID],
            &m_srcRect2, &m_desctREct2, 0, 0, SDL_FLIP_NONE);
}

void ScrollingBackground::update()
{
    if (count == maxcount){
        m_srcRect1.x += m_scrollSpeed;
        m_srcRect1.w -= m_scrollSpeed;
        m_destRect1.w -= m_scrollSpeed;


        m_scrRect2.w += m_scrollSpeed;
        m_destRect2.w += m_scrollSpeed;
        m_destRect2.x -= m_scrollSpeed;

        if (m_destRect2.w >= m_width){
            m_srcRect1.x = 0, m_srcRect1.y = 0;
            m_destRect1.x = m_position.getX();
            m_destRect1.y = m_position.getY();

            m_srcRect1.w = m_destRect1.w = m_srcREct2Width
                         = m_destRect1Width = m_width;
            m_srcRect1.h = m_destRect1.h = m_height;
        
            m_srcRect2.x = m_srcRect2.y = 0;
            m_destRect2.x = m_position.getX() + m_width;
            m_destRect2.y = m_position.getY();

            m_srcRect2.w = m_destRect2.w = m_srcRect2Width
                         = m_desctRect2Width = 0;
            m_srcRect2.h = m_desctREct2.h = m_height;
        }
        count = 0;
    }
    count++;
}


































